package diploma.kisel.modelas.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.border.EmptyBorder;

import diploma.kisel.modelas.domain.Concept;
import diploma.kisel.modelas.domain.Project;
import diploma.kisel.modelas.gui.common.CurrentVar;
import diploma.kisel.modelas.service.ConceptServiceImpl;
import diploma.kisel.modelas.service.api.ConceptService;
import diploma.kisel.modelas.validation.Validation;

/**
 * This window is designed for creating new concept and
 * saved it into database. 
 * 
 * @author Nikolay Kisel
 *
 */
public class ConceptWindow extends JFrame {

	
	private static final long serialVersionUID = 1L;

	private final int nameLength = 20;

	private JPanel contentPane;
	private JTextField textField;
	private ConceptService conceptService = new ConceptServiceImpl();

//	private static Concept currConcept;

	private static ConceptWindow frame;

	/**
	 * Create the frame.
	 */
	public ConceptWindow() {
		setTitle("Создание концепции");
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 284, 122);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);

		JLabel label = new JLabel("Название:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label, 13,
				SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, label, 10,
				SpringLayout.WEST, contentPane);
		contentPane.add(label);

		textField = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textField, -3,
				SpringLayout.NORTH, label);
		contentPane.add(textField);
		textField.setColumns(10);

		JButton button = new JButton("Сохранить");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				saveConcept();
			}
		});
		sl_contentPane.putConstraint(SpringLayout.NORTH, button, 46,
				SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, button, 63,
				SpringLayout.WEST, contentPane);
		contentPane.add(button);

		JButton button_1 = new JButton("Отменить");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frame.setVisible(false);
			}
		});
		sl_contentPane.putConstraint(SpringLayout.EAST, textField, 0,
				SpringLayout.EAST, button_1);
		sl_contentPane.putConstraint(SpringLayout.NORTH, button_1, 0,
				SpringLayout.NORTH, button);
		sl_contentPane.putConstraint(SpringLayout.WEST, button_1, 6,
				SpringLayout.EAST, button);
		contentPane.add(button_1);
	}

	public void saveConcept() {
		Concept concept = new Concept();
		String name = textField.getText();
		if (Validation.isLengthBetween(name, 1, nameLength))
			concept.setNumber(name);
		else {
			JOptionPane.showMessageDialog(frame,
					"Введено направильное название концепции. Название может содеражать до "
							+ nameLength + " символов");
			return;
		}
		Project currProject = CurrentVar.getCurrentProject();
		System.out.println(currProject);
		concept.setProject(currProject);
		conceptService.save(concept);
//		currConcept = concept;
		CurrentVar.setCurrentConcept(concept);
		JOptionPane.showMessageDialog(frame, "Концепция успешно сохранена.", "Сообщение", JOptionPane.INFORMATION_MESSAGE);
		openServerWindow();
		frame.setVisible(false);
	}

	public static ConceptWindow getFrame() {
		return frame;
	}

	public static void setFrame(ConceptWindow frame) {
		ConceptWindow.frame = frame;
	}
	
	public void openServerWindow() {
		ServerWindow serverWindow = new ServerWindow();
		ServerWindow.setFrame(serverWindow);
		serverWindow.setVisible(true);
	}

}
